*****************************************************************************************************************************************************************************************
*****************************************************************************************************************************************************************************************
****************************************************************Data analysis for moving together****************************************************************************************
*****************************************************************************************************************************************************************************************
*****************************************************************************************************************************************************************************************

*****************************************************************************************************************************************************************************************
****************************************************create data for becoming married/having child****************************************************************************************
cd "E:\workdata\704429\Projektfiles\Co-residence"
est clear
*capture log using templog.log, replace
use disc_analysis.dta, clear
drop dato2

**Calculate homogeneity**
gen homog_13     = stemt1==stemt2
replace homog_13 =. if stemt1==. | stemt2==.

gen homog_09     = stemte_20091==stemte_20092
replace homog_09 =. if stemte_20091==. | stemte_20092==.

* Create variable for couples married or with child by end 2015. 

rename pnr1 pnr
merge 1:1 pnr using E:\workdata\704429\Grunddatanewest\bef2015
drop if _merge == 2 

gen married15 = pnr2 == aegte_id

drop aegte_id-_merge
rename pnr far_id
merge 1:m far_id using E:\workdata\704429\Grunddatanewest\bef2015
drop if _merge == 2 

gen mor_partner = mor_id == pnr2

bysort far_id: egen mor_partner2 = sum(mor_partner)
bysort far_id: gen temp = _n
keep if temp == 1
replace married15 = 1 if mor_partner2 > 0 & mor_partner2 !=.

drop aegte_id-_merge mor_partner* temp pnr
rename far_id pnr1

** save data 

keep pnr1 married15
compress

rename pnr1 pnr

save married_data, replace

est clear
use analysis_out_data_all_pairs, clear

**Generate stacked datasæt**
gen id=_n
replace koen1 = koen1 - 1 
replace koen2 = koen2 - 1 

save temp_analysis1.dta, replace
foreach var in stemt residentialstability stemte_2009 pnr FOED_DAG koen  {
	rename `var'1 `var'3
	rename `var'2 `var'1
	rename `var'3 `var'2
}

append using temp_analysis1.dta

lab def koen 0 "male" 1 "female"
lab val koen1 koen
lab val koen2 koen

*Merge datasets for hetero analyses*
rename pnr1 pnr
merge 1:1 pnr using "E:\workdata\704429\Grunddatanewest\bef2014.dta", keepusing(IE_TYPE)
drop if _m==2
drop _m
recode IE_TYPE 1=0 2/3=1 // not Dane dummy

merge 1:1 pnr using "E:\workdata\704429\Grunddatanewest\udda2014.dta", keepusing(hfaudd_hovedgruppe)
drop if _m==2
drop _m

recode hfaudd_hovedgruppe 10/40=0 50/90=1 // From 3 years higher education and up is one category

gen alder_ved_valg=2013-year(FOED_DAG1) // Age in 2013
replace alder_ved_valg=alder_ved_valg-1 if month(FOED_DAG1)==12
replace alder_ved_valg=alder_ved_valg-1 if month(FOED_DAG1)==11 & day(FOED_DAG1)>19

merge 1:1 pnr using "married_data"
drop if _m==2
drop _m

*****************************************************************************************************************************************************************************************
****************************************************************************Hetero effects***********************************************************************************************
*****************************************************************************************************************************************************************************************
gen month_factor=month13+83.5  

* heterogeneuous effects in one month window 
eststo clear
eststo: reg stemt1 b84.month_factor if month13_factor >= 83 & month13_factor <= 84 , cluster(id) // Overall
eststo: reg stemt1 b84.month_factor if month13_factor >= 83 & month13_factor <= 84 & koen1==0, cluster(id) // Men
eststo: reg stemt1 b84.month_factor if month13_factor >= 83 & month13_factor <= 84 & koen1==1, cluster(id) // Women
eststo: reg stemt1 b84.month_factor if month13_factor >= 83 & month13_factor <= 84 & alder_v<=30, cluster(id)  // Low age
eststo: reg stemt1 b84.month_factor if month13_factor >= 83 & month13_factor <= 84 & alder_v>30 & alder_v<., cluster(id)  // High age
eststo: reg stemt1 b84.month_factor if month13_factor >= 83 & month13_factor <= 84 & IE==0, cluster(id)  // Dane
eststo: reg stemt1 b84.month_factor if month13_factor >= 83 & month13_factor <= 84 & IE==1, cluster(id)  // Not Dane
eststo: reg stemt1 b84.month_factor if month13_factor >= 83 & month13_factor <= 84 & hfaudd_hovedgruppe==0, cluster(id) // Short educ
eststo: reg stemt1 b84.month_factor if month13_factor >= 83 & month13_factor <= 84 & hfaudd_hovedgruppe==1, cluster(id) // Long educ
eststo: reg stemt1 b84.month_factor if month13_factor >= 83 & month13_factor <= 84 & stemte_20091==0, cluster(id) // Past non-voter
eststo: reg stemt1 b84.month_factor if month13_factor >= 83 & month13_factor <= 84 & stemte_20091==1, cluster(id) // Past voter
eststo: reg stemt1 b84.month_factor if month13_factor >= 83 & month13_factor <= 84 & married15==0, cluster(id) // not married/children 2015
eststo: reg stemt1 b84.month_factor if month13_factor >= 83 & month13_factor <= 84 & married15==1, cluster(id) // married/children 2015

esttab est1 est2 est3 using output/vote_hetero1.rtf, nostar replace se b(3)

* heterogeneuous effects in two month window 
eststo clear
eststo: reg stemt1 b85.month_factor if month13_factor >= 82 & month13_factor <= 85 , cluster(id) // Overall
eststo: reg stemt1 b85.month_factor if month13_factor >= 82 & month13_factor <= 85 & koen1==0, cluster(id) // Men
eststo: reg stemt1 b85.month_factor if month13_factor >= 82 & month13_factor <= 85 & koen1==1, cluster(id) // Women
eststo: reg stemt1 b85.month_factor if month13_factor >= 82 & month13_factor <= 85 & alder_v<=30, cluster(id)  // Low age
eststo: reg stemt1 b85.month_factor if month13_factor >= 82 & month13_factor <= 85 & alder_v>30 & alder_v<., cluster(id)  // High age
eststo: reg stemt1 b85.month_factor if month13_factor >= 82 & month13_factor <= 85 & IE==0, cluster(id)  // Dane
eststo: reg stemt1 b85.month_factor if month13_factor >= 82 & month13_factor <= 85 & IE==1, cluster(id)  // Not Dane
eststo: reg stemt1 b85.month_factor if month13_factor >= 82 & month13_factor <= 85 & hfaudd_hovedgruppe==0, cluster(id) // Short educ
eststo: reg stemt1 b85.month_factor if month13_factor >= 82 & month13_factor <= 85 & hfaudd_hovedgruppe==1, cluster(id) // Long educ
eststo: reg stemt1 b85.month_factor if month13_factor >= 82 & month13_factor <= 85 & stemte_20091==0, cluster(id) // Past non-voter
eststo: reg stemt1 b85.month_factor if month13_factor >= 82 & month13_factor <= 85 & stemte_20091==1, cluster(id) // Past voter
eststo: reg stemt1 b85.month_factor if month13_factor >= 82 & month13_factor <= 85 & married15==0, cluster(id) // not married/children 2015
eststo: reg stemt1 b85.month_factor if month13_factor >= 82 & month13_factor <= 85 & married15==1, cluster(id) // married/children 2015

esttab est1 est2 est3 using output/vote_hetero2.rtf, nostar replace se b(3)


